package com.adgroup.smartnote;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.adgroup.smartnote.SmartNoteActivity.AsynLoadAvatar;
import com.androidquery.AQuery;

import database.Database;
import ImageLoaderUrl.ImageLoader;
import JsonModel.User;
import Utilities.Configuration;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyProfileActivity extends SherlockActivity{

	TextView tvNickname;
	Button btnUpdateInfor, btnFriendList, btnLogout;
	ImageView ivAvatar;
	User u;
	private ActionBar mActionBar;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menulogin, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
		case android.R.id.title:
			finish();
			break;
		case R.id.menuSearchProfile:
			
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myprofile);
		
		//action bar
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setTitle(getResources().getString(R.string.titlemyprofile));
		
		tvNickname = (TextView) findViewById(R.id.textViewProfileNickname);
		btnFriendList = (Button) findViewById(R.id.buttonProfileFriendList);
		btnUpdateInfor = (Button) findViewById(R.id.buttonProfileUpdateInfor);
		btnLogout = (Button) findViewById(R.id.buttonProfileLogout);
		ivAvatar = (ImageView) findViewById(R.id.imageViewTitleMyProfile);
		

		u = new Database(MyProfileActivity.this).getUser();
		//load avatar;		
		AQuery aq = new AQuery(MyProfileActivity.this);
		Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
		aq.id(ivAvatar).image(u.getLink(), true,
					true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
		tvNickname.setText(u.getNickName());
		btnFriendList.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		btnUpdateInfor.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent i = new Intent(MyProfileActivity.this, UpdateInforActivity.class);
						startActivityForResult(i, Configuration.requestUpdateInfor);
					}
				});
		btnLogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new Database(MyProfileActivity.this).resetUser();
				Intent returnIntent = new Intent();
				returnIntent.putExtra("result", u.getNickName());
				setResult(RESULT_OK, returnIntent);
				finish();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==Configuration.requestUpdateInfor && resultCode==RESULT_OK){//register then login
			
		}
	}
}
